package com.wlz.curator;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 *  curator 集群 客户端
 * @author wlz
 * @date 2022-06-25  11:18 下午
 */
@Slf4j
public class CuratorClusterBaseOeerationsClient extends CuratorClusterBase{

    @Test
    public void testCluster() throws Exception{

        CuratorFramework curatorFramework = getCuratorFramework();

        String pathWithParent = "/test";

        byte[] bytes = curatorFramework.getData().forPath(pathWithParent);
        log.info("get data node : {} , data : {}", pathWithParent, new String(bytes));

        while (true) {

            try {
                byte[] bytes1 = curatorFramework.getData().forPath(pathWithParent);
                log.info("get data node : {} , data : {}", pathWithParent, new String(bytes1));

                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
                testCluster();
            }
        }

    }

    public static void main(String[] args) throws Exception{
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(5*1000, 10);

        String connecStr = "106.52.11.147:2181";
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connecStr, retryPolicy);

        curatorFramework.start();

        String pathWithParent = "/test";

        byte[] bytes = curatorFramework.getData().forPath(pathWithParent);
        log.info("get data node : {}, data: {}", pathWithParent, new String(bytes));
        while (true) {

            try {
                byte[] bytes1 = curatorFramework.getData().forPath(pathWithParent);
                log.info("get data node : {}, data: {}", pathWithParent, new String(bytes1));

                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
